home *** CD-ROM | disk | FTP | other *** search
/ Language/OS - Multiplatform Resource Library / LANGUAGE OS.iso / oper_sys / kerberos / pc / krb_libd.lha / Lib / Des / RDPASSWD.BAK < prev    next >
Encoding:
Text File  |  1991-07-18  |  4.3 KB  |  221 lines

  1. /*
  2.  * $Source: /mit/kerberos/src/lib/des/RCS/read_password.c,v $
  3.  * $Author: jon $
  4.  *
  5.  * Copyright 1985, 1986, 1987, 1988 by the Massachusetts Institute
  6.  * of Technology.
  7.  *
  8.  * For copying and distribution information, please see the file
  9.  * <mit-copyright.h>.
  10.  *
  11.  * This routine prints the supplied string to standard
  12.  * output as a prompt, and reads a password string without
  13.  * echoing.
  14.  */
  15.  
  16. #ifndef    lint
  17. static char rcsid_read_password_c[] =
  18. "$Header: read_password.c,v 4.12 89/05/30 18:01:30 jon Exp $";
  19. #endif    lint
  20.  
  21. #include <mit_copy.h>
  22. #include <des.h>
  23. #include "conf.h"
  24.  
  25. #include <stdio.h>
  26. #ifdef    BSDUNIX
  27. #include <strings.h>
  28. #include <sys/ioctl.h>
  29. #include <signal.h>
  30. #include <setjmp.h>
  31. #else
  32. char     *strcpy();
  33. int      strcmp();
  34. #ifndef index
  35. char     *index();
  36. #endif
  37. #endif
  38.  
  39. #ifdef    BSDUNIX
  40. static jmp_buf env;
  41. #endif
  42.  
  43. #ifdef BSDUNIX
  44. #ifdef POSIX
  45. typedef void sigtype;
  46. #else
  47. typedef int sigtype;
  48. #endif
  49. static sigtype sig_restore();
  50. static push_signals(), pop_signals();
  51. int des_read_pw_string();
  52. #endif
  53.  
  54. #ifdef IBMPC
  55. #include <conio.h>
  56. #endif
  57. /*** Routines ****************************************************** */
  58. int
  59. des_read_password(k,prompt,verify)
  60.     des_cblock *k;
  61.     char *prompt;
  62.     int    verify;
  63. {
  64.     int ok;
  65.     char key_string[BUFSIZ];
  66.  
  67. #ifdef BSDUNIX
  68.     if (setjmp(env)) {
  69.     ok = -1;
  70.     goto lose;
  71.     }
  72. #endif
  73.  
  74.     ok = des_read_pw_string(key_string, BUFSIZ, prompt, verify);
  75.     if (ok == 0)
  76.     des_string_to_key(key_string, k);
  77.  
  78. lose:
  79.     memset(key_string,0, sizeof (key_string));
  80.     return ok;
  81. }
  82.  
  83. #ifdef IBMPC
  84. static void gets_no_echo(char *s,int size)
  85. {
  86.     char *end=s+size-1,*p=s;
  87.     while (s<end && (*s=getch()) != '\r')
  88.         s++;
  89.     *s='\0';
  90.     putch('\r');
  91.     putch('\n');
  92. }
  93. #endif
  94.  
  95. /*
  96.  * This version just returns the string, doesn't map to key.
  97.  *
  98.  * Returns 0 on success, non-zero on failure.
  99.  */
  100.  
  101. int
  102. des_read_pw_string(s,max,prompt,verify)
  103.     char *s;
  104.     int    max;
  105.     char *prompt;
  106.     int    verify;
  107. {
  108.     int ok = 0;
  109.     char *ptr;
  110.     
  111. #ifdef BSDUNIX
  112.     jmp_buf old_env;
  113.     struct sgttyb tty_state;
  114. #endif
  115.     char key_string[BUFSIZ];
  116.  
  117.     if (max > BUFSIZ) {
  118.     return -1;
  119.     }
  120.  
  121. #ifdef    BSDUNIX
  122.     /* XXX assume jmp_buf is typedef'ed to an array */
  123.     bcopy((char *)old_env, (char *)env, sizeof(env));
  124.     if (setjmp(env))
  125.     goto lose;
  126.  
  127.     /* save terminal state*/
  128.     if (ioctl(0,TIOCGETP,(char *)&tty_state) == -1) 
  129.     return -1;
  130.  
  131.     push_signals();
  132.     /* Turn off echo */
  133.     tty_state.sg_flags &= ~ECHO;
  134.     if (ioctl(0,TIOCSETP,(char *)&tty_state) == -1)
  135.     return -1;
  136. #endif
  137.     while (!ok) {
  138.     (void) printf(prompt);
  139.     (void) fflush(stdout);
  140. #ifdef    IBMPC
  141.     gets_no_echo(s,max);
  142.     if (!strlen(s))
  143.         continue;
  144. #else
  145.     if (!fgets(s, max, stdin)) {
  146.         clearerr(stdin);
  147.         continue;
  148.     }
  149.     if ((ptr = index(s, '\n')))
  150.         *ptr = '\0';
  151. #endif
  152.     if (verify) {
  153.         printf("\nVerifying, please re-enter %s",prompt);
  154.         (void) fflush(stdout);
  155. #ifdef IBMPC
  156.         gets_no_echo(key_string,sizeof(key_string));
  157.         if (!strlen(key_string))
  158.         continue;
  159. #else
  160.         if (!fgets(key_string, sizeof(key_string), stdin)) {
  161.         clearerr(stdin);
  162.         continue;
  163.         }
  164.             if ((ptr = index(key_string, '\n')))
  165.         *ptr = '\0';
  166. #endif
  167.         if (strcmp(s,key_string)) {
  168.         printf("\n\07\07Mismatch - try again\n");
  169.         (void) fflush(stdout);
  170.         continue;
  171.         }
  172.     }
  173.     ok = 1;
  174.     }
  175.  
  176. #ifdef    BSDUNIX
  177. lose:
  178.     if (!ok)
  179.     bzero(s,0, max);
  180.     printf("\n");
  181.     /* turn echo back on */
  182.     tty_state.sg_flags |= ECHO;
  183.     if (ioctl(0,TIOCSETP,(char *)&tty_state))
  184.     ok = 0;
  185.     pop_signals();
  186.     bcopy((char *)env, (char *)old_env, sizeof(env));
  187. #endif
  188.     if (verify)
  189.     memset(key_string, 0,sizeof (key_string));
  190.     s[max-1] = 0;        /* force termination */
  191.     return !ok;            /* return nonzero if not okay */
  192. }
  193.  
  194. #ifdef    BSDUNIX
  195. /*
  196.  * this can be static since we should never have more than
  197.  * one set saved....
  198.  */
  199. static sigtype (*old_sigfunc[NSIG])();
  200.  
  201. static push_signals()
  202. {
  203.     register i;
  204.     for (i = 0; i < NSIG; i++)
  205.     old_sigfunc[i] = signal(i,sig_restore);
  206. }
  207.  
  208. static pop_signals()
  209. {
  210.     register i;
  211.     for (i = 0; i < NSIG; i++)
  212.     (void) signal(i,old_sigfunc[i]);
  213. }
  214.  
  215. static sigtype
  216. sig_restore()
  217. {
  218.     longjmp(env,1);
  219. }
  220. #endif
  221.